//
//  ListenViewController.m
//  news
//
//  Created by dllo on 16/3/16.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "ListenViewController.h"
#import "PrefixHeader.pch"

@interface ListenViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *arrTemp;
//@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSString *str;
@property (nonatomic, strong)NSString *strrr;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSNumber *num;
@end

@implementation ListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"音乐";
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 ) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    /** 刷新加载方法 **/
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableView.mj_header endRefreshing];
        });
        
    }];
    self.tableView.mj_header = header;
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self dataFooter];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
    
    self.tabBarController.tabBar.hidden = YES;
    /** 数据获取 **/
    _str = @"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=android&pageId=1&pageSize=20&status=0&tagName=%E7%B2%BE%E9%80%89%7C%E6%AD%8C%E5%8D%95";
//    _page = 2;
        [NewWorking GetDayaWithURL:_str dic:nil sussess:^(id responseObject) {
            self.strrr = responseObject[@"pageId"];
           _dataArr = [ListenModel modelHanderWithArray:responseObject[@"list"]];
            /** 上拉加载**/
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(dataFooter)];
            [self.tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
    
    // 重用池
    [_tableView registerClass:[ListenTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (void)dataFooter{
     NSString *str = @"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=android&pageId=";
     NSString *strOne =@"&pageSize=20&status=0&tagName=%E7%B2%BE%E9%80%89%7C%E6%AD%8C%E5%8D%95";
    NSString *strTow = [NSString stringWithFormat:@"%@%ld%@",str, _page, strOne];
    
   [AFNetBlock GetDataWithURL:strTow block:^(id block) {
       self.strrr = block[@"pageId"];
       _arrTemp= [ListenModel modelHanderWithArray:block[@"list"]];
       for (ListenModel *dic in _arrTemp) {
           [_dataArr addObject:dic];
       }
       
        self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(dataFooter)];
       [_tableView reloadData];
   }];
    _page++;

}
#pragma mark   -  tableView 设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ListenModel *m = _dataArr[indexPath.row];
    [cell.mainImgView sd_setImageWithURL:[NSURL URLWithString:m.albumCoverUrl290]];
    cell.titleLabel.text = m.title;
    cell.infoLabel.text =m.lastUptrackTitle;
    cell.countImgView.image = [UIImage imageNamed:@"count"];
    cell.playImgView.image = [UIImage imageNamed:@"play"];
    if (m.playsCounts.floatValue  > 100000000) {
        NSString *str = [NSString stringWithFormat:@"%.1f亿",m.playsCounts.floatValue / 100000000];
        cell.numberLabel.text = str;
        
    }else if(m.playsCounts.floatValue > 10000){
        
        NSString *str  = [NSString stringWithFormat:@"%.1f万", m.playsCounts.floatValue / 10000];
        
        cell.numberLabel.text = str;
    }else{
        cell.numberLabel.text = [NSString stringWithFormat:@"%f", m.playsCounts.floatValue];
        
    }
    cell.countLabel.text = [NSString stringWithFormat:@"%f集", m.playsCounts.floatValue];
    
    return cell;
    
}
#pragma mark - cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
#pragma mark - cell 的点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ListenXqViewController *xq = [[ListenXqViewController alloc]init];
    xq.urlStr =_str;
    
    xq.model = _dataArr[indexPath.row];
    
    [self.navigationController pushViewController:xq animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
